package com.psa.element;

import android.content.ContentValues;
import android.database.Cursor;
import com.google.gson.Gson;
import com.psa.tool.DBTool;

/**
 *
 * @author david
 * @date 2014-11-21
 * ID int Primary Key,Target nvarchar(30),Requirement nvarchar(2),Status nvarchar(2),Contents nvarchar(max),Message nvarchar(max)--/*
 */
public class Interchange implements IJson,ISqlObject {
    public int ID = 0;
    public String Target = "";
    public String Requirement = "";
    public String Status = "";
    public String Contents = "";
    public String Message = "";

    @Override
    public String toJson() {
        return new Gson().toJson(this);
    }

    @Override
    public int insert(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("ID", ID);
        values.put("Target", Target.trim());
        values.put("Requirement", Requirement.trim());
        values.put("Status", Status.trim());
        values.put("Contents", Contents.trim());
        values.put("Message", Message.trim());
        return (int)dbTool.insert("Interchange", values);
    }

    @Override
    public int update(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("Target", Target.trim());
        values.put("Requirement", Requirement.trim());
        values.put("Status", Status.trim());
        values.put("Contents", Contents.trim());
        values.put("Message", Message.trim());
        String where = "ID = ?";
        String[] whereval = { String.valueOf(ID) };
        return dbTool.update("Interchange", values, where, whereval);
    }

    @Override
    public int delete(DBTool dbTool) {
        String where = "ID = ?";
        String[] whereval = { String.valueOf(ID) };
        return dbTool.delete("Interchange", where, whereval);
    }

    @Override
    public ISqlObject full(Cursor cursor) {
        if (cursor!=null && cursor.moveToNext()) {
            Interchange interchange = new Interchange();
            interchange.ID = cursor.getInt(0);
            interchange.Target = cursor.getString(1);
            interchange.Requirement = cursor.getString(2);
            interchange.Status = cursor.getString(3);
            interchange.Contents = cursor.getString(4);
            interchange.Message = cursor.getString(5);
            return interchange;
        }
        return null;
    }

    @Override
    public void clear(DBTool dbTool) {
        dbTool.delete("Interchange", "ID Like ?", new String[]{"%"});
    }

    @Override
    public String getDataSql() {
        return "Interchange";
    }

    @Override
    public String getSyncMethod() {
        return "getdataobjects";
    }
}
